package com.example.springboot.controller;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.example.springboot.pojo.Location;
import com.example.springboot.service.LocationService;
import com.example.springboot.pojo.Result;
@RestController
@CrossOrigin
@RequestMapping("/location")
public class LocationController {
    @Autowired
    private LocationService locationService;
    @PostMapping("/create")
    public Result create(@RequestBody Location location) {
        locationService.create(location);
        return Result.success();
    }
    @DeleteMapping("/delete")
    public Result delete(Integer id) {
        locationService.delete(id);
        return Result.success();
    }
    @GetMapping("/findAll")
    public Result findAll() {
        List<Location> locations = locationService.findAll();
        return Result.success(locations);
    }
    @GetMapping("/findById")
    public Result findById(Integer id) {
        Location location = locationService.findById(id);
        return Result.success(location);
    }
    @PostMapping("/update")
    public Result update(@RequestBody Location location) {
        locationService.update(location);
        return Result.success();
    }

    @GetMapping("/findByName")
    public Result findByType(String name)
    {
        List<Location> locations=locationService.findByName(name);
        return Result.success(locations);
    }

    @GetMapping("/selectPage")
    public Result selectPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Location> page = locationService.selectPage(pageNum, pageSize);
        return Result.success(page);
    }
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        locationService.deleteBatch(ids);
        return Result.success();
    }


}
